<template>
  <h2 class="headTitle">个人信息</h2>
  <validate-form v-if="!isEdit">
    <div class="mb-3">
      <label class="form-label">姓名</label>
      <div class="form-control">{{ account.name }}</div>
    </div>
    <div class="mb-3">
      <label class="form-label">姓别</label>
      <div class="form-control">{{ account.sex }}</div>
    </div>
    <div class="mb-3">
      <label class="form-label">出生日期</label>
      <div class="form-control">{{ account.birth }}</div>
    </div>
    <template #submit>
      <button type="button" class="btn btn-primary" @click="isEdit = !isEdit">修改信息</button>
    </template>
  </validate-form>
  <validate-form v-if="isEdit">
    <div class="mb-3">
      <label class="form-label">姓名</label>
      <validate-input
        v-model="account.name"
      ></validate-input>
    </div>
    <div class="mb-3">
      <label class="form-label">姓别</label>
      <validate-input
        v-model="account.sex"
      ></validate-input>
    </div>
    <div class="mb-3">
      <label class="form-label">出生日期</label>
      <validate-input
        v-model="account.birth"
      ></validate-input>
    </div>
    <template #submit>
      <button type="submit" class="btn btn-primary" @click="isEdit = !isEdit; editInformation()">提交</button>
    </template>
  </validate-form>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import ValidateForm from '@/components/ValidateForm.vue'
import ValidateInput from '@/components/ValidateInput.vue'
import { useStore } from 'vuex'
import { AccountProps } from '@/store'
import axios from 'axios'

export default defineComponent({
  name: 'EditInfo',
  components: {
    ValidateInput,
    ValidateForm
  },
  setup () {
    // axios.post('/mock/edit').then()
    const store = useStore()
    const isEdit = ref(false)
    const account = reactive<AccountProps>({
      name: store.state.account.name,
      sex: store.state.account.sex,
      birth: store.state.account.birth
    })
    const editInformation = () => {
      store.dispatch('editInfo', account)
    }
    return {
      account,
      isEdit,
      editInformation
    }
  }
})
</script>

<style scoped lang="less">
@media all {
  font-size: 15px;
}

.headTitle {
  font-size: 2rem;
  display: flex;
  justify-content: space-around;
}
</style>
